<?php
require('../include/init.php');
require('check_login.php');
require('../include/pagenavigator.php');
$act = $_REQUEST['act'];
if($act == 'add') {
	$cate_id = $_POST['cate_id'] ? $_POST['cate_id'] : 0;
	$product_name = trim($_POST['product_name']);
	$product_description = str_replace("'", "''", $_POST['product_description']);
	$recommend = $_POST['recommend'];
	$rand = rand(0, 99999);
	//上传产品图片
	$fileType = substr($_FILES['product_images']['name'], strpos($_FILES['product_images']['name'], '.'));
	$Oname = $rand.'_O_'.$time.$fileType;    //原图片名
	$Sname = $rand.'_S_'.$time.$fileType;	//缩略图片名
	$OimagePath = $productImgPath.$Oname;  //原图地址
	$SimagePath =$productImgPath.$Sname;  //缩略图地址	
	if(is_uploaded_file($_FILES['product_images']['tmp_name'])){
		if(!move_uploaded_file($_FILES['product_images']['tmp_name'], $OimagePath)) {
			die('文件上传失败');
		} else {
			resize($OimagePath,$SimagePath,158,117);
		}
	}

	if($product_name) {
		$GLOBALS['db']->query("insert into kyz_products(cate_id, product_name, product_description, product_images, views, recommend, create_time) values($cate_id, '$product_name', '$product_description', '$Sname', 0, $recommend, $time)");
		header("Location: $_SERVER[PHP_SELF]");
	}
} elseif ($act == 'edit') {
	$product_id = $_GET['product_id'];
	$products = $GLOBALS['db']->getRow("select * from kyz_products where product_id='$product_id' limit 1");
	$cate_list = get_product_category();
	$smarty->assign('cate_list', $cate_list);	
	$smarty->assign('show_edit_form', true);
	$smarty->assign('products', $products);
} elseif ($act == 'save') {
	$product_id = $_POST['product_id'];
	$cate_id = $_POST['cate_id'] ? $_POST['cate_id'] : 0;
	$product_name = trim($_POST['product_name']);
	$product_description = str_replace("'", "''", $_POST['product_description']);
	$o_product_images = $_POST['o_product_images'];
	$recommend = $_POST['recommend'];
	$rand = rand(0, 99999);
	//上传产品图片
	$fileType = substr($_FILES['product_images']['name'], strpos($_FILES['product_images']['name'], '.'));
	$Oname = $rand.'_O_'.$time.$fileType;    //原图片名
	$Sname = $rand.'_S_'.$time.$fileType;	//缩略图片名
	$OimagePath = $productImgPath.$Oname;  //原图地址
	$SimagePath =$productImgPath.$Sname;  //缩略图地址	
	$set = '';
	if(is_uploaded_file($_FILES['product_images']['tmp_name'])){
		if(!move_uploaded_file($_FILES['product_images']['tmp_name'], $OimagePath)) {
			die('文件上传失败');
		} else {
			resize($OimagePath,$SimagePath,158,117);
			if(!empty($o_product_images)) {
				$product_S_images = $o_product_images;
				$product_O_images = str_replace('S', 'O', $o_product_images);
				@unlink($productImgPath.$product_S_images);
				@unlink($productImgPath.$product_O_images);			
			}
			$set .= ", product_images='". $Sname."' ";			
		}
	}
	if($product_id) {
		$GLOBALS['db']->query("update kyz_products set cate_id='$cate_id', product_name='$product_name', product_description='$product_description', recommend='$recommend'{$set}where product_id='$product_id'");
		header("Location: $_SERVER[PHP_SELF]");
	}
} elseif ($act == 'del') {
	$product_id = $_GET['product_id'];
	$product_images = $GLOBALS['db']->getOne("select product_images from kyz_products where product_id='$product_id' limit 1");
	if($product_images) {
		$product_S_images = $product_images;
		$product_O_images = str_replace('S', 'O', $product_images);
		@unlink($productImgPath.$product_S_images);
		@unlink($productImgPath.$product_O_images);		
	}
	$GLOBALS['db']->query("delete from kyz_products where product_id='$product_id'");
	echo 1;exit();
} elseif ($act == 'del_img') { 
	$product_id = $_GET['product_id'];
	$product_images = $GLOBALS['db']->getOne("select product_images from kyz_products where product_id='$product_id' limit 1");
	if($product_images) { 
		$GLOBALS['db']->query("update kyz_products set product_images='' where product_id='$product_id'");
		$product_S_images = $product_images;
		$product_O_images = str_replace('S', 'O', $product_images);
		@unlink($productImgPath.$product_S_images);
		@unlink($productImgPath.$product_O_images);
	}
	echo 1;exit();
	
} elseif ($act == 'checkExist') {
	$product_name = $_GET['product_name'];
	$o_product_name = preg_replace('/\s+/', '', $_GET['o_product_name']);
	if($product_name != $o_product_name) {
		$num = $GLOBALS['db']->getOne("select count(*) from kyz_products where product_name='$product_name' limit 1");
		if($num > 0) {
			echo 'false';
		} else {
			echo 'true';
		}
	} else {
		echo 'true';
	}
	exit();
} elseif ($act == 'recommend') { 
	$product_id = $_GET['product_id'];
	$recommend = $_GET['recommend'];
	if($product_id) { 
		$GLOBALS['db']->query("update kyz_products set recommend='$recommend' where product_id='$product_id'");
	}
	exit();
} else {
	if($act == 'show_add_form') {
		$cate_list = get_product_category();
		$smarty->assign('cate_list', $cate_list);		
		$smarty->assign('show_add_form', true);	
	}
	$pagesize = 20;
	$page = $_GET['page'] ? $_GET['page'] : 1;
	$start = ($page-1) * $pagesize;	
	$product_list = $GLOBALS['db']->getAll("select product_id, product_name, product_images, recommend from kyz_products order by create_time desc limit $start, $pagesize");
	//print_r($product_list);exit();
	$total = $GLOBALS['db']->getOne("select count(*) from kyz_products");
	$pagenavigator = new PageNavigator(array('total' => $total, 'currentPage' => $page, 'pageSize' => $pagesize, 'pageTemplate' => "?page={page}",'url'=>''));	
	$smarty->assign('pagenavigator', $pagenavigator);	
	$smarty->assign('product_list', $product_list);

}

$smarty->display('admin/product_manage.html');
?>